package com.hxzy.controller;

import com.hxzy.common.domain.AjaxResult;
import com.hxzy.common.page.TableDataInfo;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.DictType;
import com.hxzy.entity.HouseTypes;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.DictTypeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : DictTypeController
 * @Author : ctx
 * @Date: 2021/9/17 14:29
 */
@Controller
@RequestMapping("admin")
public class DictTypeController extends BaseController {
    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping(value = "/dictType/list")
    public String showPage(Model model){
        return "system/dictType/list";
    }

    /**
     * 返回给前端bootstrap-table的数据
     * @return
     */
    // @RequiresPermissions(value = "system/role/list")
    @ResponseBody
    @PostMapping(value = "/dictType")
    public TableDataInfo ajaxData(DictType dictType, HttpServletRequest request){
        super.startPage(request);
        //编写方法，查询
        List<DictType> dictTypeList = dictTypeService.selectCondition(dictType);
        return super.getDataTable(dictTypeList);
    }
    /*
     * 显示新增页面
     * */
    @GetMapping(value = "/dictType/add")
    public String addPage(Model model){
        return "system/dictType/add";
    }
    /*
     * 新增
     * */
    @ResponseBody
    // @RequiresPermissions(value = "system/role/add")
    @PostMapping(value = "/dictType/add")
    public AjaxResult addRole(DictType dictType){
        AdminUser adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();
        dictType.setCreateBy(adminUser.getLoginUserName());
        dictType.setCreateTime(new Date());
        dictType.setUpdateBy(adminUser.getLoginUserName());
        dictType.setUpdateTime(new Date());

        return toAjax(dictTypeService.insert(dictType));
    }
    /**
     * 显示编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping(value = "/dictType/edit/{id}")
    public String editPage(@PathVariable(value = "id") Long id, ModelMap model){
        System.out.println(id);
        DictType dictType = dictTypeService.selectByPrimaryKey(id);
        System.out.println(dictType==null);
        //把数据转给页面
        model.put("dictType", dictType);
        return "system/dictType/edit";
    }

    /**
     * 修改保存
     */
    @ResponseBody
    // @RequiresPermissions(value = "system/role/edit")
    @PostMapping(value = "/dictType/edit")
    public AjaxResult editData(DictType dictType){
        AdminUser adminUser= (AdminUser) SecurityUtils.getSubject().getPrincipal();

        dictType.setUpdateTime(new Date());
        dictType.setUpdateBy(adminUser.getLoginUserName());
        //一定要调用updateByPrimaryKeySelective 选择性更新，
        // updateByPrimaryKey 全部列更新，因为修改时修，新增时间和新增人未赋值
        int count=this.dictTypeService.updateByPrimaryKeySelective(dictType);
        return super.toAjax(count);
    }

    @ResponseBody
    @GetMapping("/dictType/existsName")
    public int existsName(Long dictId,String dictName){
        int i = dictTypeService.existsName(dictId, dictName);
        return i;
    }
}
